use std::{
    cmp,
    collections::{BTreeMap, HashMap, HashSet},
    time::Duration,
};

use ruma::{
    api::client::{
        filter::{FilterDefinition, LazyLoadOptions},
        sync::sync_events::{
            self,
            v3::{
                Filter, GlobalAccountData, InviteState, InvitedRoom, JoinedRoom, Presence, Rooms,
                ToDevice,
            },
            DeviceLists,
        },
        uiaa::UiaaResponse,
    },
    events::StateEventType,
    serde::Raw,
    DeviceId, OwnedUserId, RoomId, UserId,
};

use crate::{core::pdu::count::PduCount, server::server, Result, Ruma, RumaResponse};

/// # `GET /_matrix/client/r0/sync`
///
/// Synchronize the client's state with the latest state on the server.
///
/// - This endpoint takes a `since` parameter which should be the `next_batch`
///   value from a previous request for incremental syncs.
///
/// Calling this endpoint without a `since` parameter returns:
/// - Some of the most recent events of each timeline
/// - Notification counts for each room
/// - Joined and invited member counts, heroes
/// - All state events
///
/// Calling this endpoint with a `since` parameter from a previous `next_batch`
/// returns: For joined rooms:
/// - Some of the most recent events of each timeline that happened after since
/// - If user joined the room after since: All state events (unless lazy loading
///   is activated) and all device list updates in that room
/// - If the user was already in the room: A list of all events that are in the
///   state now, but were not in the state at `since`
/// - If the state we send contains a member event: Joined and invited member
///   counts, heroes
/// - Device list updates that happened after `since`
/// - If there are events in the timeline we send or the user send updated his
///   read mark: Notification counts
/// - EDUs that are active now (read receipts, typing updates, presence)
/// - TODO: Allow multiple sync streams to support Pantalaimon
///
/// For invited rooms:
/// - If the user was invited after `since`: A subset of the state of the room
///   at the point of the invite
///
/// For left rooms:
/// - If the user left after `since`: `prev_batch` token, empty state (TODO:
///   subset of the state at the point of the leave)
pub async fn sync_events_route(
    body: Ruma<sync_events::v3::Request>,
) -> Result<sync_events::v3::Response, RumaResponse<UiaaResponse>> {
    let sender_user = body.sender_user.expect("user is authenticated");
    let sender_device = body.sender_device.expect("user is authenticated");
    let body = body.body;

    // Presence update
    // if services.globals.allow_local_presence() {
    //     services
    //         .presence
    //         .ping_presence(&sender_user, &body.set_presence)?;
    // }

    // Setup watchers, so if there's no response, we can wait for them
    let watcher = server().global.watch(&sender_user, &sender_device);

    let next_batch = server().global.current_count();
    let next_batchcount = PduCount::Normal(next_batch);
    let next_batch_string = next_batch.to_string();

    // Load filter
    let filter = match body.filter {
        None => FilterDefinition::default(),
        Some(Filter::FilterDefinition(filter)) => filter,
        Some(Filter::FilterId(filter_id)) => server()
            .users
            .get_filter(&sender_user, &filter_id)?
            .unwrap_or_default(),
    };

    // some clients, at least element, seem to require knowledge of redundant
    // members for "inline" profiles on the timeline to work properly
    // let (lazy_load_enabled, lazy_load_send_redundant) = match filter.room.state.lazy_load_options {
    //     LazyLoadOptions::Enabled {
    //         include_redundant_members,
    //     } => (true, include_redundant_members),
    //     LazyLoadOptions::Disabled => (false, cfg!(feature = "element_hacks")),
    // };

    let full_state = body.full_state;

    let mut joined_rooms = BTreeMap::new();
    let since = body
        .since
        .as_ref()
        .and_then(|string| string.parse().ok())
        .unwrap_or(0);
    let sincecount = PduCount::Normal(since);

    let mut presence_updates: HashMap<OwnedUserId, ruma::events::presence::PresenceEvent> =
        HashMap::new();
    let mut left_encrypted_users: HashSet<OwnedUserId> = HashSet::new(); // Users that have left any encrypted rooms the sender was in
    let mut device_list_updates = HashSet::new();
    let mut device_list_left = HashSet::new();

    // Look for device list updates of this account
    // device_list_updates.extend(
    //     services
    //         .users
    //         .keys_changed(sender_user.as_ref(), since, None)
    //         .filter_map(Result::ok),
    // );

    // if services.globals.allow_local_presence() {
    //     process_presence_updates(&services, &mut presence_updates, since, &sender_user).await?;
    // }

    let all_joined_rooms = server().rooms.state_cache.joined_rooms(&sender_user)?;

    // Coalesce database writes for the remainder of this scope.
    // let _cork = services.db.cork_and_flush();

    // TODO: Implement load_joined_room
    // for room_id in all_joined_rooms {
    //     let room_id = room_id;
    //     if let Ok(joined_room) = load_joined_room(
    //         &services,
    //         &sender_user,
    //         &sender_device,
    //         &room_id,
    //         since,
    //         sincecount,
    //         next_batch,
    //         next_batchcount,
    //         // lazy_load_enabled,
    //         // lazy_load_send_redundant,
    //         full_state,
    //         &mut device_list_updates,
    //         &mut left_encrypted_users,
    //     )
    //     .await
    //     {
    //         if !joined_room.is_empty() {
    //             joined_rooms.insert(room_id.clone(), joined_room);
    //         }
    //     }
    // }

    let mut left_rooms = BTreeMap::new();

    let mut invited_rooms = BTreeMap::new();
    let all_invited_rooms: Vec<_> = server().rooms.invited_rooms(&sender_user)?;
    for result in all_invited_rooms {
        let (room_id, invite_state_events) = result;

        // Get and drop the lock to wait for remaining operations to finish
        // let insert_lock = server().rooms.timeline.mutex_insert.lock(&room_id).await;
        // drop(insert_lock);

        // let invite_count = server()
        //     .rooms
        //     .state_cache
        //     .get_invite_count(&room_id, &sender_user)?;

        // // Invited before last sync
        // if Some(since) >= invite_count {
        //     continue;
        // }

        invited_rooms.insert(
            room_id.clone(),
            InvitedRoom {
                invite_state: InviteState {
                    events: invite_state_events,
                },
            },
        );
    }
    // TODO: Implement left_encrypted_users
    // for user_id in left_encrypted_users {
    //     let dont_share_encrypted_room = services
    //         .rooms
    //         .user
    //         .get_shared_rooms(vec![sender_user.clone(), user_id.clone()])?
    //         .filter_map(Result::ok)
    //         .filter_map(|other_room_id| {
    //             Some(
    //                 services
    //                     .rooms
    //                     .state_accessor
    //                     .room_state_get(&other_room_id, &StateEventType::RoomEncryption, "")
    //                     .ok()?
    //                     .is_some(),
    //             )
    //         })
    //         .all(|encrypted| !encrypted);
    //     // If the user doesn't share an encrypted room with the target anymore, we need
    //     // to tell them
    //     if dont_share_encrypted_room {
    //         device_list_left.insert(user_id);
    //     }
    // }

    // Remove all to-device events the device received *last time*
    // services
    //     .users
    //     .remove_to_device_events(&sender_user, &sender_device, since)?;

    let response = sync_events::v3::Response {
        next_batch: next_batch_string,
        rooms: Rooms {
            leave: left_rooms,
            join: joined_rooms,
            invite: invited_rooms,
            knock: BTreeMap::new(), // TODO
        },
        presence: Presence {
            events: presence_updates
                .into_values()
                .map(|v| Raw::new(&v).expect("PresenceEvent always serializes successfully"))
                .collect(),
        },
        account_data: GlobalAccountData {
            events: vec![], // services
                            //     .account_data
                            //     .changes_since(None, &sender_user, since)?
                            //     .into_iter()
                            //     .filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Global))
                            //     .collect(),
        },
        device_lists: DeviceLists {
            changed: device_list_updates.into_iter().collect(),
            left: device_list_left.into_iter().collect(),
        },
        device_one_time_keys_count: BTreeMap::new(),
        // services
        //     .users
        //     .count_one_time_keys(&sender_user, &sender_device)?,
        to_device: ToDevice {
            events: vec![],
            // services
            //     .users
            //     .get_to_device_events(&sender_user, &sender_device)?,
        },
        // Fallback keys are not yet supported
        device_unused_fallback_key_types: None,
    };

    // TODO: Retry the endpoint instead of returning
    if !full_state
        && response.rooms.is_empty()
        && response.presence.is_empty()
        && response.account_data.is_empty()
        && response.device_lists.is_empty()
        && response.to_device.is_empty()
    {
        // Hang a few seconds so requests are not spammed
        // Stop hanging if new info arrives
        let default = Duration::from_secs(30);
        let duration = cmp::min(body.timeout.unwrap_or(default), default);
        _ = tokio::time::timeout(duration, watcher).await;
    }

    Ok(response)
}

// async fn load_joined_room(
//     sender_user: &UserId,
//     sender_device: &DeviceId,
//     room_id: &RoomId,
//     since: u64,
//     sincecount: PduCount,
//     next_batch: u64,
//     next_batchcount: PduCount,
//     lazy_load_enabled: bool,
//     lazy_load_send_redundant: bool,
//     full_state: bool,
//     device_list_updates: &mut HashSet<OwnedUserId>,
//     left_encrypted_users: &mut HashSet<OwnedUserId>,
// ) -> Result<JoinedRoom> {
//     // Get and drop the lock to wait for remaining operations to finish
//     // This will make sure the we have all events until next_batch
//     let insert_lock = services.rooms.timeline.mutex_insert.lock(room_id).await;
//     drop(insert_lock);

//     let (timeline_pdus, limited) = load_timeline(services, sender_user, room_id, sincecount, 10)?;

//     // let send_notification_counts = !timeline_pdus.is_empty()
//     //     || services
//     //         .rooms
//     //         .user
//     //         .last_notification_read(sender_user, room_id)?
//     //         > since;

//     let mut timeline_users = HashSet::new();
//     for (_, event) in &timeline_pdus {
//         timeline_users.insert(event.sender.as_str().to_owned());
//     }

//     // services
//     //     .rooms
//     //     .lazy_loading
//     //     .lazy_load_confirm_delivery(sender_user, sender_device, room_id, sincecount)
//     //     .await?;

//     // Database queries:

//     let Some(current_shortstatehash) = services.rooms.state.get_room_shortstatehash(room_id)?
//     else {
//         return Err!(Database(error!("Room {room_id} has no state")));
//     };

//     let since_shortstatehash = services
//         .rooms
//         .user
//         .get_token_shortstatehash(room_id, since)?;

//     let (heroes, joined_member_count, invited_member_count, joined_since_last_sync, state_events) =
//         if timeline_pdus.is_empty() && since_shortstatehash == Some(current_shortstatehash) {
//             // No state changes
//             (Vec::new(), None, None, false, Vec::new())
//         } else {
//             // Calculates joined_member_count, invited_member_count and heroes
//             let calculate_counts = || {
//                 let joined_member_count = services
//                     .rooms
//                     .state_cache
//                     .room_joined_count(room_id)?
//                     .unwrap_or(0);
//                 let invited_member_count = services
//                     .rooms
//                     .state_cache
//                     .room_invited_count(room_id)?
//                     .unwrap_or(0);

//                 // Recalculate heroes (first 5 members)
//                 let mut heroes: Vec<OwnedUserId> = Vec::with_capacity(5);

//                 if joined_member_count.saturating_add(invited_member_count) <= 5 {
//                     // Go through all PDUs and for each member event, check if the user is still
//                     // joined or invited until we have 5 or we reach the end

//                     for hero in services
//                         .rooms
//                         .timeline
//                         .all_pdus(sender_user, room_id)?
//                         .filter_map(Result::ok) // Ignore all broken pdus
//                         .filter(|(_, pdu)| pdu.kind == TimelineEventType::RoomMember)
//                         .map(|(_, pdu)| {
//                             let content: RoomMemberEventContent =
//                                 serde_json::from_str(pdu.content.get()).map_err(|_| {
//                                     Error::bad_database("Invalid member event in database.")
//                                 })?;

//                             if let Some(state_key) = &pdu.state_key {
//                                 let user_id = UserId::parse(state_key.clone()).map_err(|_| {
//                                     Error::bad_database("Invalid UserId in member PDU.")
//                                 })?;

//                                 // The membership was and still is invite or join
//                                 if matches!(
//                                     content.membership,
//                                     MembershipState::Join | MembershipState::Invite
//                                 ) && (services.rooms.state_cache.is_joined(&user_id, room_id)?
//                                     || services.rooms.state_cache.is_invited(&user_id, room_id)?)
//                                 {
//                                     Ok::<_, Error>(Some(user_id))
//                                 } else {
//                                     Ok(None)
//                                 }
//                             } else {
//                                 Ok(None)
//                             }
//                         })
//                         .filter_map(Result::ok)
//                         // Filter for possible heroes
//                         .flatten()
//                     {
//                         if heroes.contains(&hero) || hero == sender_user {
//                             continue;
//                         }

//                         heroes.push(hero);
//                     }
//                 }

//                 Ok::<_, Error>((
//                     Some(joined_member_count),
//                     Some(invited_member_count),
//                     heroes,
//                 ))
//             };

//             let since_sender_member: Option<RoomMemberEventContent> = since_shortstatehash
//                 .and_then(|shortstatehash| {
//                     services
//                         .rooms
//                         .state_accessor
//                         .state_get(
//                             shortstatehash,
//                             &StateEventType::RoomMember,
//                             sender_user.as_str(),
//                         )
//                         .transpose()
//                 })
//                 .transpose()?
//                 .and_then(|pdu| {
//                     serde_json::from_str(pdu.content.get())
//                         .map_err(|_| Error::bad_database("Invalid PDU in database."))
//                         .ok()
//                 });

//             let joined_since_last_sync = since_sender_member
//                 .map_or(true, |member| member.membership != MembershipState::Join);

//             if since_shortstatehash.is_none() || joined_since_last_sync {
//                 // Probably since = 0, we will do an initial sync

//                 let (joined_member_count, invited_member_count, heroes) = calculate_counts()?;

//                 let current_state_ids = services
//                     .rooms
//                     .state_accessor
//                     .state_full_ids(current_shortstatehash)
//                     .await?;

//                 let mut state_events = Vec::new();
//                 let mut lazy_loaded = HashSet::new();

//                 let mut i: u8 = 0;
//                 for (shortstatekey, id) in current_state_ids {
//                     let (event_type, state_key) = services
//                         .rooms
//                         .short
//                         .get_statekey_from_short(shortstatekey)?;

//                     if event_type != StateEventType::RoomMember {
//                         let Some(pdu) = services.rooms.timeline.get_pdu(&id)? else {
//                             error!("Pdu in state not found: {}", id);
//                             continue;
//                         };
//                         state_events.push(pdu);

//                         i = i.wrapping_add(1);
//                         if i % 100 == 0 {
//                             tokio::task::yield_now().await;
//                         }
//                     } else if !lazy_load_enabled
//                 || full_state
//                 || timeline_users.contains(&state_key)
//                 // TODO: Delete the following line when this is resolved: https://github.com/vector-im/element-web/issues/22565
//                 || (cfg!(feature = "element_hacks") && *sender_user == state_key)
//                     {
//                         let Some(pdu) = services.rooms.timeline.get_pdu(&id)? else {
//                             error!("Pdu in state not found: {}", id);
//                             continue;
//                         };

//                         // This check is in case a bad user ID made it into the database
//                         if let Ok(uid) = UserId::parse(&state_key) {
//                             lazy_loaded.insert(uid);
//                         }
//                         state_events.push(pdu);

//                         i = i.wrapping_add(1);
//                         if i % 100 == 0 {
//                             tokio::task::yield_now().await;
//                         }
//                     }
//                 }

//                 // Reset lazy loading because this is an initial sync
//                 services
//                     .rooms
//                     .lazy_loading
//                     .lazy_load_reset(sender_user, sender_device, room_id)?;

//                 // The state_events above should contain all timeline_users, let's mark them as
//                 // lazy loaded.
//                 services
//                     .rooms
//                     .lazy_loading
//                     .lazy_load_mark_sent(
//                         sender_user,
//                         sender_device,
//                         room_id,
//                         lazy_loaded,
//                         next_batchcount,
//                     )
//                     .await;

//                 (
//                     heroes,
//                     joined_member_count,
//                     invited_member_count,
//                     true,
//                     state_events,
//                 )
//             } else {
//                 // Incremental /sync
//                 let since_shortstatehash = since_shortstatehash.unwrap();

//                 let mut delta_state_events = Vec::new();

//                 if since_shortstatehash != current_shortstatehash {
//                     let current_state_ids = services
//                         .rooms
//                         .state_accessor
//                         .state_full_ids(current_shortstatehash)
//                         .await?;
//                     let since_state_ids = services
//                         .rooms
//                         .state_accessor
//                         .state_full_ids(since_shortstatehash)
//                         .await?;

//                     for (key, id) in current_state_ids {
//                         if full_state || since_state_ids.get(&key) != Some(&id) {
//                             let Some(pdu) = services.rooms.timeline.get_pdu(&id)? else {
//                                 error!("Pdu in state not found: {}", id);
//                                 continue;
//                             };

//                             delta_state_events.push(pdu);
//                             tokio::task::yield_now().await;
//                         }
//                     }
//                 }

//                 let encrypted_room = services
//                     .rooms
//                     .state_accessor
//                     .state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")?
//                     .is_some();

//                 let since_encryption = services.rooms.state_accessor.state_get(
//                     since_shortstatehash,
//                     &StateEventType::RoomEncryption,
//                     "",
//                 )?;

//                 // Calculations:
//                 let new_encrypted_room = encrypted_room && since_encryption.is_none();

//                 let send_member_count = delta_state_events
//                     .iter()
//                     .any(|event| event.kind == TimelineEventType::RoomMember);

//                 if encrypted_room {
//                     for state_event in &delta_state_events {
//                         if state_event.kind != TimelineEventType::RoomMember {
//                             continue;
//                         }

//                         if let Some(state_key) = &state_event.state_key {
//                             let user_id = UserId::parse(state_key.clone()).map_err(|_| {
//                                 Error::bad_database("Invalid UserId in member PDU.")
//                             })?;

//                             if user_id == sender_user {
//                                 continue;
//                             }

//                             let new_membership = serde_json::from_str::<RoomMemberEventContent>(
//                                 state_event.content.get(),
//                             )
//                             .map_err(|_| Error::bad_database("Invalid PDU in database."))?
//                             .membership;

//                             match new_membership {
//                                 MembershipState::Join => {
//                                     // A new user joined an encrypted room
//                                     if !share_encrypted_room(
//                                         services,
//                                         sender_user,
//                                         &user_id,
//                                         room_id,
//                                     )? {
//                                         device_list_updates.insert(user_id);
//                                     }
//                                 }
//                                 MembershipState::Leave => {
//                                     // Write down users that have left encrypted rooms we are in
//                                     left_encrypted_users.insert(user_id);
//                                 }
//                                 _ => {}
//                             }
//                         }
//                     }
//                 }

//                 if joined_since_last_sync && encrypted_room || new_encrypted_room {
//                     // If the user is in a new encrypted room, give them all joined users
//                     device_list_updates.extend(
//                         services
//                             .rooms
//                             .state_cache
//                             .room_members(room_id)
//                             .flatten()
//                             .filter(|user_id| {
//                                 // Don't send key updates from the sender to the sender
//                                 sender_user != user_id
//                             })
//                             .filter(|user_id| {
//                                 // Only send keys if the sender doesn't share an encrypted room with the target
//                                 // already
//                                 !share_encrypted_room(services, sender_user, user_id, room_id)
//                                     .unwrap_or(false)
//                             }),
//                     );
//                 }

//                 let (joined_member_count, invited_member_count, heroes) = if send_member_count {
//                     calculate_counts()?
//                 } else {
//                     (None, None, Vec::new())
//                 };

//                 let mut state_events = delta_state_events;
//                 let mut lazy_loaded = HashSet::new();

//                 // Mark all member events we're returning as lazy-loaded
//                 for pdu in &state_events {
//                     if pdu.kind == TimelineEventType::RoomMember {
//                         match UserId::parse(
//                             pdu.state_key
//                                 .as_ref()
//                                 .expect("State event has state key")
//                                 .clone(),
//                         ) {
//                             Ok(state_key_userid) => {
//                                 lazy_loaded.insert(state_key_userid);
//                             }
//                             Err(e) => error!("Invalid state key for member event: {}", e),
//                         }
//                     }
//                 }

//                 // Fetch contextual member state events for events from the timeline, and
//                 // mark them as lazy-loaded as well.
//                 for (_, event) in &timeline_pdus {
//                     if lazy_loaded.contains(&event.sender) {
//                         continue;
//                     }

//                     if !services.rooms.lazy_loading.lazy_load_was_sent_before(
//                         sender_user,
//                         sender_device,
//                         room_id,
//                         &event.sender,
//                     )? || lazy_load_send_redundant
//                     {
//                         if let Some(member_event) = services.rooms.state_accessor.room_state_get(
//                             room_id,
//                             &StateEventType::RoomMember,
//                             event.sender.as_str(),
//                         )? {
//                             lazy_loaded.insert(event.sender.clone());
//                             state_events.push(member_event);
//                         }
//                     }
//                 }

//                 services
//                     .rooms
//                     .lazy_loading
//                     .lazy_load_mark_sent(
//                         sender_user,
//                         sender_device,
//                         room_id,
//                         lazy_loaded,
//                         next_batchcount,
//                     )
//                     .await;

//                 (
//                     heroes,
//                     joined_member_count,
//                     invited_member_count,
//                     joined_since_last_sync,
//                     state_events,
//                 )
//             }
//         };

//     // Look for device list updates in this room
//     device_list_updates.extend(
//         services
//             .users
//             .keys_changed(room_id.as_ref(), since, None)
//             .filter_map(Result::ok),
//     );

//     let notification_count = if send_notification_counts {
//         Some(
//             services
//                 .rooms
//                 .user
//                 .notification_count(sender_user, room_id)?
//                 .try_into()
//                 .expect("notification count can't go that high"),
//         )
//     } else {
//         None
//     };

//     let highlight_count = if send_notification_counts {
//         Some(
//             services
//                 .rooms
//                 .user
//                 .highlight_count(sender_user, room_id)?
//                 .try_into()
//                 .expect("highlight count can't go that high"),
//         )
//     } else {
//         None
//     };

//     let prev_batch = timeline_pdus
//         .first()
//         .map_or(Ok::<_, Error>(None), |(pdu_count, _)| {
//             Ok(Some(match pdu_count {
//                 PduCount::Backfilled(_) => {
//                     error!("timeline in backfill state?!");
//                     "0".to_owned()
//                 }
//                 PduCount::Normal(c) => c.to_string(),
//             }))
//         })?;

//     let room_events: Vec<_> = timeline_pdus
//         .iter()
//         .map(|(_, pdu)| pdu.to_sync_room_event())
//         .collect();

//     let mut edus: Vec<_> = services
//         .rooms
//         .read_receipt
//         .readreceipts_since(room_id, since)
//         .filter_map(Result::ok) // Filter out buggy events
//         .map(|(_, _, v)| v)
//         .collect();

//     if services.rooms.typing.last_typing_update(room_id).await? > since {
//         edus.push(
//             serde_json::from_str(
//                 &serde_json::to_string(&services.rooms.typing.typings_all(room_id).await?)
//                     .expect("event is valid, we just created it"),
//             )
//             .expect("event is valid, we just created it"),
//         );
//     }

//     // Save the state after this sync so we can send the correct state diff next
//     // sync
//     services.rooms.user.associate_token_shortstatehash(
//         room_id,
//         next_batch,
//         current_shortstatehash,
//     )?;

//     Ok(JoinedRoom {
//         account_data: RoomAccountData {
//             events: services
//                 .account_data
//                 .changes_since(Some(room_id), sender_user, since)?
//                 .into_iter()
//                 .filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Room))
//                 .collect(),
//         },
//         summary: RoomSummary {
//             heroes,
//             joined_member_count: joined_member_count.map(ruma_from_u64),
//             invited_member_count: invited_member_count.map(ruma_from_u64),
//         },
//         unread_notifications: UnreadNotificationsCount {
//             highlight_count,
//             notification_count,
//         },
//         timeline: Timeline {
//             limited: limited || joined_since_last_sync,
//             prev_batch,
//             events: room_events,
//         },
//         state: RoomState {
//             events: state_events
//                 .iter()
//                 .map(|pdu| pdu.to_sync_state_event())
//                 .collect(),
//         },
//         ephemeral: Ephemeral { events: edus },
//         unread_thread_notifications: BTreeMap::new(),
//     })
// }
